package com.peanut.web.controller.dataquality;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.peanut.model.CompanyTreeNode;
import com.peanut.model.DataQualityForm;
import com.peanut.model.User;
import com.peanut.service.DataQualityService;
import com.peanut.util.DateUtil;
import com.peanut.util.Pagination;
import com.peanut.util.web.WebUtil;
import com.peanut.web.controller.BaseController;
import com.peanut.web.views.SingleSheetExcelView;

@Controller
@RequestMapping(value = "/dataquality")
public class DataQualityController extends BaseController {
	private DataQualityService dataQualityService;
	
	@Autowired
	public void setDataQualityService(DataQualityService dataQualityService) {
		this.dataQualityService = dataQualityService;
	}

	@RequestMapping(value = "/newestProblemMonthView")
	public ModelAndView newestProblemMonthView(ModelAndView mav, HttpServletRequest request) {
		mav.addObject("userCompany", getUserCompany(request));
		return mav;
	}
	
	@RequestMapping(value = "/conventionDataView")
	public ModelAndView conventionDataView(ModelAndView mav, HttpServletRequest request) {
		mav.addObject("userCompany", getUserCompany(request));
		return mav;
	}
	
	@RequestMapping(value = "/consistencyDataView")
	public ModelAndView consistencyDataView(ModelAndView mav, HttpServletRequest request) {
		mav.addObject("userCompany", getUserCompany(request));
		return mav;
	}
	
	private CompanyTreeNode getUserCompany(HttpServletRequest request) {
		User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String comcode = user.getCompanyCode();
		Map param = new HashMap();
		param.put("comcode", comcode);
		List<HashMap<String, String>> nodeList = dataQualityService.queryCompanyByCode(param);
		CompanyTreeNode rootNode = new CompanyTreeNode();
		if (nodeList != null && nodeList.size() > 0) {
			HashMap<String, String> node = nodeList.get(0);
			rootNode.setComcode(node.get("comcode").trim());
			rootNode.setComcname(node.get("comcname").trim());
			rootNode.setLeaf(BooleanUtils.toBoolean(node.get("leaf")));
			rootNode.setGradelevel(node.get("gradelevel").trim());
			rootNode.setUppcomcode(node.get("uppcomcode").trim());
			rootNode.setText("["+rootNode.getComcode()+"]"+rootNode.getComcname());
		}
		return rootNode;
	}
	
	@RequestMapping(value = "/assessmentQueryView")
	public ModelAndView assessmentQueryView(ModelAndView mav, HttpServletRequest request) {
		mav.addObject("userCompany", getUserCompany(request));
		return mav;
	}
	
	@RequestMapping(value = "/queryNewestInCompany") 
	@ResponseBody
	public Pagination<HashMap> queryNewestProblemInCompany(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if ("S".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "D");
			} else if ("D".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "Z");
			} else if ("Z".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "0");
			}
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryNewestProblemInCompany(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryNewestProblemInCompanyCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryNewestInProduct") 
	@ResponseBody
	public Pagination<HashMap> queryNewestProblemInProduct(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2,3" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryNewestProblemInProduct(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryNewestProblemInProductCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryNewestInCheckPoint") 
	@ResponseBody
	public Pagination<HashMap> queryNewestProblemInCheckPoint(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2,3" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryNewestProblemInCheckPoint(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryNewestProblemInCheckPointCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryConventionInCompany") 
	@ResponseBody
	public Pagination<HashMap> queryConventionDataInCompany(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if ("S".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "D");
			} else if ("D".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "Z");
			} else if ("Z".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "0");
			}
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryConventionDataInCompany(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryConventionDataInCompanyCount(param));
		return page;
	}

	@RequestMapping(value = "/queryConventionInProduct") 
	@ResponseBody
	public Pagination<HashMap> queryConventionDataInProduct(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryConventionDataInProduct(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryConventionDataInProductCount(param));
		return page;
	}

	@RequestMapping(value = "/queryConventionInCheckPoint") 
	@ResponseBody
	public Pagination<HashMap> queryConventionDataInCheckPoint(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryConventionDataInCheckPoint(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryConventionDataInCheckPointCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryConsistencyInCompany") 
	@ResponseBody
	public Pagination<HashMap> queryConsistencyDataInCompany(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if ("S".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "D");
			} else if ("D".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "Z");
			} else if ("Z".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "0");
			}
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryConsistencyDataInCompany(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryConsistencyDataInCompanyCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryConsistencyInCheckPoint") 
	@ResponseBody
	public Pagination<HashMap> queryConsistencyDataInCheckPoint(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if ("S".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "D");
			} else if ("D".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "Z");
			} else if ("Z".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "0");
			}
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryConsistencyDataInCheckPoint(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryConsistencyDataInCheckPointCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryAssessmentInCompany") 
	@ResponseBody
	public Pagination<HashMap> queryAssessmentInCompany(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2,3" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if ("S".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "D");
			} else if ("D".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "Z");
			} else if ("Z".equals(form.getGradeLevel().trim())) {
				param.put("lowLevel", "0");
			}
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			} else {
				param.put("departCode", "PUB");
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryAssessmentInCompany(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryAssessmentInCompanyCount(param));
		return page;
	}

	@RequestMapping(value = "/queryAssessmentInProduct") 
	@ResponseBody
	public Pagination<HashMap> queryAssessmentInProduct(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2,3" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryAssessmentInProduct(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryAssessmentInProductCount(param));
		return page;
	}

	@RequestMapping(value = "/queryAssessmentInCheckPoint") 
	@ResponseBody
	public Pagination<HashMap> queryAssessmentInCheckPoint(DataQualityForm form, Pagination<HashMap> page, HttpServletRequest request) {
		Map param = new HashMap();
		String sortColumn = page.getSort() == null ? "1,2,3" : page.getSort();
		param.put("sort", sortColumn);
		param.put("dir", page.getDir());
		
		if (form.getCheckDate() != null) {
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			param.put("comcode", form.getComcode().trim());
			param.put("comLevel", form.getGradeLevel().trim());
			if (form.getProduct() != null && "".equals(form.getProduct().trim()) == false
					&& "--".equals(form.getProduct().trim()) == false) {
				param.put("departCode", form.getProduct().trim());
			} else {
				param.put("departCode", "PUB");
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
			}
		}
		page.setPagingRows(dataQualityService.queryAssessmentInCheckPoint(param, page.getStart(), page.getLimit()));
		page.setTotalRows(dataQualityService.queryAssessmentInCheckPointCount(param));
		return page;
	}
	
	@RequestMapping(value = "/queryDateRecord")
	@ResponseBody
	public List<HashMap<String, String>> queryDateRecord() {
		return dataQualityService.queryDateRecord();
	}
	
	@RequestMapping(value = "/queryProduct")
	@ResponseBody
	public List<HashMap<String, String>> queryProductList() {
		List<HashMap<String, String>> resultList = dataQualityService.queryProduct();
		HashMap<String, String> emptyMap = new HashMap<String, String>();
		emptyMap.put("departcode", "--");
		emptyMap.put("departname", "----");
		resultList.add(0, emptyMap);
		return resultList;
	}
	
	@RequestMapping(value = "/queryCompanyByCode")
	@ResponseBody
	public CompanyTreeNode queryCompanyByCode(@RequestParam String comcode) {
		Map param = new HashMap();
		param.put("comcode", comcode);
		List<HashMap<String, String>> nodeList = dataQualityService.queryCompanyByCode(param);
		CompanyTreeNode rootNode = new CompanyTreeNode();
		if (nodeList != null && nodeList.size() > 0) {
			HashMap<String, String> node = nodeList.get(0);
			rootNode.setComcode(node.get("comcode").trim());
			rootNode.setComcname(node.get("comcname").trim());
			rootNode.setLeaf(BooleanUtils.toBoolean(node.get("leaf")));
			rootNode.setGradelevel(node.get("gradelevel").trim());
			rootNode.setUppcomcode(node.get("uppcomcode").trim());
			rootNode.setText("["+rootNode.getComcode()+"]"+rootNode.getComcname());
		}
		return rootNode;
	}
	
	@RequestMapping(value = "/queryProvince")
	@ResponseBody
	public List<HashMap<String, String>> queryProvince() {
		return dataQualityService.queryProvince();
	}
	
	@RequestMapping(value = "/queryCity")
	@ResponseBody
	public List<HashMap<String, String>> queryCity(DataQualityForm form) {
		Map param = new HashMap();
		//param.put("provinceCode", form.getProvince());
		List<HashMap<String, String>> resultList = dataQualityService.queryCity(param);
		HashMap<String, String> emptyMap = new HashMap<String, String>();
		emptyMap.put("comcode", "--");
		emptyMap.put("comcname", "----");
		resultList.add(0, emptyMap);
		return resultList;
	}
	
	@RequestMapping(value = "/queryCounty")
	@ResponseBody
	public List<HashMap<String, String>> queryCounty(DataQualityForm form) {
		Map param = new HashMap();
		//param.put("cityCode", form.getCity());
		List<HashMap<String, String>> resultList = dataQualityService.queryCounty(param);
		HashMap<String, String> emptyMap = new HashMap<String, String>();
		emptyMap.put("comcode", "--");
		emptyMap.put("comcname", "----");
		resultList.add(0, emptyMap);
		return resultList;
	}
	
	@RequestMapping(value = "/companyTree")
	@ResponseBody
	public List<CompanyTreeNode> queryTreeNode(CompanyTreeNode companyNode) {
		List<CompanyTreeNode> treeList = dataQualityService.createCompanyTree(companyNode);
		return treeList;
	}
	
	@RequestMapping(value = "/queryCheckPoint")
	@ResponseBody
	public List<HashMap<String, String>> queryCheckPointList(DataQualityForm form, @RequestParam String isConsistency) {
		List<HashMap<String, String>> resultList = null;
		Map param = new HashMap();
		if ("true".equals(isConsistency)) {
			param.put("isConsistency", "true");
		}
		if (form.getCheckDate() == null) {
			//首次打开页面时，获取初始值
			resultList = dataQualityService.queryInitCheckPoint(param);
		} else {
			//选择产品线或公司变化时
			if (form.getProduct() != null && "--".equals(form.getProduct().trim()) == false) {
				param.put("departcode", form.getProduct().trim());
			}
			if (form.getComcode() != null && "45000000".equals(form.getComcode()) == false) {
				param.put("comcode", form.getComcode().trim());
			}
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			resultList = dataQualityService.queryCheckPointList(param);
		}
		HashMap<String, String> emptyMap = new HashMap<String, String>();
		emptyMap.put("checkpoint", "--");
		emptyMap.put("checkpointname", "----");
		resultList.add(0, emptyMap);
		return resultList;
	}
	
	/**
	 * 导出doExcelView
	 * 
	 * @param model
	 * @param projectId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/doExcelView")
	public ModelAndView doExcelView(ModelMap model, HttpServletRequest request) {
		List headerList = new ArrayList();
		headerList.add("test1");
		headerList.add("test2");
		model.put("title", "测试sheet");
		model.put("filename", "测试Excel");
		model.put("headerList", headerList);
		List dataList = new ArrayList();
		dataList.add("data1");
		dataList.add("data2");
		model.put("dataList", dataList);
		return new ModelAndView(new SingleSheetExcelView(), model);
	}
	
	/**
	 * 导出doExcelView
	 * 
	 * @param model
	 * @param projectId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/doExcelSelf")
	public void doExcelSelf(DataQualityForm form, HttpServletRequest request, HttpServletResponse response) 
				throws Exception {
		ModelMap model = new ModelMap();
		OutputStream out = response.getOutputStream();
		String strMsg = "找不到可导出的数据。。。请核查";
		if (form.getCheckDate() != null) {
			Map param = new HashMap();
			param.put("startDate", form.getCheckDate().split(",")[0]);
			param.put("endDate", form.getCheckDate().split(",")[1]);
			if (form.getComcode().trim() != null && "45000000".equals(form.getComcode().trim()) == false) {
				param.put("comcode", form.getComcode().trim());
			}
			if (form.getCheckPoint() != null && "".equals(form.getCheckPoint().trim()) == false
					&& "--".equals(form.getCheckPoint().trim()) == false) {
				param.put("checkPoint", form.getCheckPoint().trim());
				param.put("flag", "1");
			}
			List<HashMap<String, String>> tableInfoList = dataQualityService.queryPointTableInfo(param);
			if (tableInfoList != null && tableInfoList.size() > 0) {
				WritableWorkbook workbook =  Workbook.createWorkbook(out);
				WritableSheet sheet = workbook.createSheet(form.getCheckDate().replace(",", "~"), 0);
				
				List<HashMap<String, String>> headerList = dataQualityService.queryPointHeaderInfo(param);
				String headerArray[] = {"checkpoint", "chkstartdate", "chkenddate", "checkdate", "uppcomcode", 
						"comcode", "riskcode", "businessno", "chkoccurdate"};
				HashMap<String, String> headerMap = headerList.get(0);
				int iHeaderCount = Integer.parseInt(String.valueOf(headerMap.get("setcount")));
				//导出表头
				int iHeaderCols = 0;
				String strHeaderValue = null;
				for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
					if (iHeaderCols < 9) {
						strHeaderValue = headerMap.get(headerArray[iHeaderCols]);
					} else {
						strHeaderValue = headerMap.get("colnam" + (iHeaderCols - 8));
					}
					sheet.addCell(new Label(iHeaderCols, 0, strHeaderValue + ""));
					iHeaderCols++;
					if (iHeaderCols > iHeaderCount - 1) {
						break;
					}
				}
				
				//导出数据
				param.put("tableName", tableInfoList.get(0).get("tablename"));
				Long totalDataCount = dataQualityService.queryWrongListForExportCount(param);
				if (totalDataCount > 0 && totalDataCount < 65535) {
					int limit = 1000;
					int curPageNum = 0;
					int pageCount = 0;
					int pageCountMod = totalDataCount.intValue() % limit;
					if (pageCountMod == 0) {
						pageCount = totalDataCount.intValue() / limit;
					} else {
						pageCount = totalDataCount.intValue() / limit + 1;
					}
					//从EXCEL的第一行开始写数据
					int rows = 1;
					for (curPageNum = 0; curPageNum < pageCount; curPageNum ++) {
						List<HashMap<String, String>> dataList = dataQualityService.queryWrongListForExport(param, curPageNum * limit, limit);
						String strLabelValue = null;
						HashMap<String, String> tempDataMap = null;
						int iDataSize = dataList.size();
						String pattern="00";
						java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
						for (int i = 0; i < iDataSize; i++) {
							tempDataMap = dataList.get(i);
							for (int cols = 0; cols < iHeaderCount; cols++) {
								if (cols < 9) {
									strLabelValue = String.valueOf(tempDataMap.get(headerArray[cols]));
								} else {
									strLabelValue = String.valueOf(tempDataMap.get("column" + (df.format(cols - 8))));
								}
								sheet.addCell(new Label(cols, rows, strLabelValue));
							}
							rows++;
						}
						dataList = null;
					}
					//设置下载时客户端Excel的名称   
					String filename = form.getComcode().trim() + "_" + form.getCheckPoint().trim()
					+ "_" + DateUtil.getDateTime(new Date(), "yyyy-MM-dd") 
					+ ".xls"; 
					filename = WebUtil.encodeFilename(filename, request);//处理中文文件名  
					response.setContentType("application/vnd.ms-excel");     
					response.setHeader("Content-disposition", "attachment;filename=" + filename);
					//写入response
					workbook.write();
					out.flush();
					workbook.close();
				} else {
					if (totalDataCount > 65535) {
						out.write("导出的数据量超出了EXCEL的65535限制。".getBytes());
						out.flush();
					}
				}
			} else {
				out.write(strMsg.getBytes());
				out.flush();
			}
		} else {
			out.write(strMsg.getBytes());
			out.flush();
		}
	}
}
